<?php

namespace app\system\controller;

use app\common\model\MemberModel;

class MemberController extends BaseController
{

  /**
   * 无需登录的方法
   * @var array
   */
  protected $noLogin = [];

  /**
   * 不需要检测权限的方法
   * @var array
   */
  protected $noCheck = [];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
    '*' => 'index'
  ];

  /**
   * 当前模型
   * @var null
   */
  protected $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new MemberModel();
  }

  /**
   * 会员首页
   * @return array|string
   * @throws \Exception
   */
  public function index()
  {
    if ($this->request->isAjax()) {
      return $this->getPage();
    }
    return $this->fetch();
  }

  /**
   * 添加
   * @return string
   * @throws \Exception
   */
  public function add()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'mobile|手机号' => 'require',
          'nick|昵称' => 'require',
          'unionid|唯一ID' => 'require',
      ]);
      $this->model->add($data);
      $this->success('操作成功');
    }
    return $this->fetch();
  }

  /**
   * 编辑
   * @return string
   * @throws \Exception
   */
  public function edit()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'mobile|手机号' => 'require',
          'nick|昵称' => 'require',
          'unionid|唯一ID' => 'require',
      ]);
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $id   = $this->request->param('id/d');
    $info = $this->model->where('id', $id)->find();
    $this->assign('info', $info);
    return $this->fetch();
  }

  /**
   * 删除
   * @return string
   * @throws \Exception
   */
  public function del()
  {
    $id = $this->request->param('id');
    $this->model->del($id);
    $this->success('操作成功');
  }
}
